package net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import util.Parser;
import util.Protocol;

public class BackClient extends Thread
{
	final int num = 521;
	
	final static int port = 8888;
	
	final static String ServerIp = "localhost";
	
	Protocol protocol = new Protocol();
	
	Parser parser = new Parser(); 
	
	static Socket tcpcon;
	
	String receivemassage;
	
	byte[] sentence = new byte[num];
	
	byte[] asentence = new byte[num];
	
	static InputStream inputStream;
	
	static OutputStream outputStream;
	
	static
	{
		try 
		{
			tcpcon = new Socket(ServerIp,port);
			
			inputStream = tcpcon.getInputStream();
			outputStream = tcpcon.getOutputStream();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void SendMassage(String message)
	{
		try 
		{
			if(message.equals("byebye"))
			{
				outputStream.write(message.getBytes());
				inputStream.close();
				outputStream.close();
				tcpcon.close();
			}
			else
			{
				outputStream.write(message.getBytes());
			}
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public String ReseiveMassage()
	{
		try 
		{
			int count = inputStream.read(sentence);
			String receivemassage = new String(sentence,0,count);
			System.out.println(receivemassage);
			return receivemassage;
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		return null;
		
	}
	
	public String ReseiveFriendMassage()
	{
		while(true)
		{
			try 
			{
				int count = inputStream.read(sentence);
				String receivemassage = new String(sentence,0,count);
				return receivemassage;
			} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		return null;
		}
	}
	
}
